<?php
/**********************************************************************************
 
 	Checks to see if any icons are missing from the database and downloads
	any icons missing in your folders.
	
	Copyright (C) 2010 Benjamin Thomas
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 	Author: Benjamin Thomas
	Email: thomas.ben@gmail.com
 	Created: 06.22.10
 	Revised: 07.03.10
 
	Inputs: graph_limit - Number of days to display on graph (default is 7)
			table_limit - Number of days to display in table (default is 30)
			item - Item to search for
 	Calls: 
 
 ***********************************************************************************
 */
require_once(dirname(__FILE__) . '/../lib/MarketDB.php');
require_once(dirname(__FILE__) . '/../lib/netFunctions.php');

if (array_key_exists("confirm", $_GET)) { $confirm = true; } else { $confirm = false; }
if (array_key_exists("download", $_GET)) {	$download = true; } else {	$download = false; }
if (array_key_exists("update", $_GET)) { $update = true; } else { $update = false; }

if ($confirm == false && ($download == true || $update == true)) {
	error('You must select the confirmation box before you can update the database or download icons');
}
if ($confirm == true && ($download == false && $update == false)) {
	error('You must select to update the database and/or download icons.');
}

if ($confirm == false || ($download == false && $update == false)) {
	$db = new MarketDB();
	$sql = "SELECT itemid FROM items WHERE icon = ''";
	$emptyIcons = $db->publicQuery($sql);

	echo count($emptyIcons) . ' items found without icons.<BR>';

	$sql = "SELECT DISTINCT icon FROM items";
	$iconSet = $db->publicQuery($sql);
	echo count($iconSet) . ' unique icons found in your database.<BR>';
	$notfound = 0;
	foreach($iconSet as &$val) {
		$fileName = "images/icons/" . $val['icon'];
		if (file_exists($fileName)) {
		} else {
			$notfound++;
		}
	}
	echo  $notfound . ' icons were not found in your images folder.<BR>';

	echo "<BR><BR>";
	if (count($emptyIcons) == 0 && $notfound == 0) {
		echo "All icons are downloaded and inputed into the database.";
		return;
	}

	
	echo '<form action="?">';
	if (count($emptyIcons)) { echo '<input type="checkbox" name="update">I want to update the icons in the database<BR>'; }
	if (count($emptyIcons) || $notfound) { echo '<input type="checkbox" name="download">I want to download any icons I am missing (Non functional)<BR>'; }
	echo '<input type="checkbox" name="confirm">I understand this may take a long time.<BR>';
	echo '<input type="submit"></form>';
	return;
} 

if ($update) {
	$db = new MarketDB();
	$sql = "SELECT itemid FROM items WHERE icon = ''";
	$emptyIcons = $db->publicQuery($sql);
	unset($db);
	echo "Attempting to update " . count($emptyIcons) . ' items found without icons.<BR>';
	foreach($emptyIcons as &$val) {
	
		$id = $val['itemid'];
		echo "Searching for icon for item ($id).<BR>"; ob_flush();
		$result = getItemData($id);
		$icon = $result['icon'];
		echo "Adding icon ( $icon ) for itemid ($id)<BR>"; ob_flush();
		$sql = "UPDATE items SET icon = '$icon' WHERE itemid = $id";
		$db->publicQuery($sql);
	}
	echo "Finished updating database.<BR>";
}

if ($download) {
	$db = new MarketDB();
	$sql = "SELECT DISTINCT icon FROM items";
	$iconSet = $db->publicQuery($sql);
	unset($db);
	foreach($iconSet as &$val) {
		$fileName = "images/icons/" . $val['icon'];
		if (!file_exists($fileName)) {
			echo "Fetching http://wow.allakhazam.com/images/icons/" . $val['icon']; ob_flush();
			save_image('http://wow.allakhazam.com/images/icons/' . $val['icon'], $fileName);
			//Sleeps for 1/3 a second to avoid spamming server.
			usleep(300000);
	
		}
	}
	echo "Finished downloading icons.<BR>";
}

function save_image($inPath,$outPath)
{ //Download images from remote server
    $in=    fopen($inPath, "rb");
    $out=   fopen($outPath, "wb");
    while ($chunk = fread($in,8192))
    {
        fwrite($out, $chunk, 8192);
    }
    fclose($in);
    fclose($out);
}

function error($str) {
	echo '<div style="background-color: #fee; border: 2px dashed red; padding: 5px"><b>Important</b><br /><br />' . $str . '</div>';
}

?>

